<?php
namespace app\index\controller;
use app\index\model\User as UserModel;
use app\index\validate\User as UserValidate;
use app\index\model\Log as LogModel;
use think\Db;
class User extends Common
{
  /** 
   * 会员添加
   */
  public function add()
  {
    if(request()->isAjax()){
      $error = (new UserValidate())->goCheck();
      if($error !== true){
        $this->error($error);
      }
      $data = input('post.');
      $user = new UserModel();
      if($user->allowField(true)->save($data)){
        $this->success('添加成功');
      }else{
        $this->error('添加失败');
      }
      return;
    }
    return $this->fetch();
  }

  /**
   * 会员列表
   */
  public function lst()
  {
    $post = input('get');
    if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$post)) {
      $map['name'] = input('get');
    }
    else
    {
      if (strlen($post) > 0 and strlen($post) < 11 and $post) {
      $map['id'] = input('get');
      }
      if (strlen($post) == 11) {
        $map['tel'] = input('get');
      }
      if (strlen($post) > 11) {
        $this->error('输入位数有误');
      }
    }
    $user = empty($map) ? Db::name('user')->order('sort desc')->order('id asc')->paginate(20) : db('user')->where($map)->order('sort desc')->order('id asc')->paginate($listRows = 20, $simple = false, $config = ['query'=>array('get'=>$post)]);
    $this->assign('user',$user);
    return $this->fetch();
  }

  //排序
  public function orders()
  {
    if(request()->isPost()) {
      $post = $this->request->post();
      $i = 0;
      foreach ($post['id'] as $k => $val) {
        $order = UserModel::where('id',$val)->value('sort');
        if($order != $post['sort'][$k]) {
          if(false == UserModel::where('id',$val)->update(['sort'=>$post['sort'][$k]])) {
            return $this->error('更新失败');
          } else {
            $i++;
          }
        }
      }
      return $this->success('更新成功','User/lst');
    }
  }

  /**
   * 会员修改
   */
  public function edit($id)
  {
    $code = db('user')->find($id);
    if(request()->isAjax()){
      $error = (new UserValidate())->goCheck();
      if($error !== true){
        $this->error($error);
      }
      $data = input('post.');
      $user = new UserModel();
      $res = $user->allowField(true)->save($data,['id'=>input('id')]);
      if($res !== false){
        $this->success('修改成功');
      }else{
        $this->error('修改失败');
      }
      return;
    }
    if(!$code){
      $this->error("该用户不存在",'lst');
    }
    $this->assign('user',$code);
    return view();
  }

  //消费
  public function spend($id)
  {
    $code = db('user')->find($id);
    if(request()->isAjax()){
      $data = input('post.');
      $user = new UserModel();
      $consume = $data['balance'];
      $num = $code['balance']-$data['balance'];
      $data['balance'] = $num;
      $jiber = $code['integral']+$consume;
      $data['integral'] = $jiber;
      $res = $user->allowField(true)->save($data,['id'=>input('id')]);
      if($res !== false){
        //写入日志
        addLog($uid = $user->id , $caozuo = '消费' , $balance = $consume);
        addLog($uid = $user->id , $caozuo = '积分充值' , $balance = $consume);
        $this->success('消费成功');
      }else{
        $this->error('消费失败');
      }
      return;
    }
    if(!$code){
      $this->error("该用户不存在",'lst');
    }
    $this->assign('user',$code);
    return view();
  }

  //充值
  public function Recharge($id)
  {
    $code = db('user')->find($id);
    if(request()->isAjax()){
      $data = input('post.');
      $user = new UserModel();
      $recharge = $data['balance'];
      $num = $code['balance']+$data['balance'];
      $data['balance'] = $num;
      $res = $user->allowField(true)->save($data,['id'=>input('id')]);
      if($res !== false){
        //写入日志
        addLog($uid = $user->id , $caozuo = '充值' , $balance = $recharge);
        $this->success('充值成功');
      }else{
        $this->error('消费失败');
      }
      return;
    }
    if(!$code){
      $this->error("该用户不存在",'lst');
    }
    $this->assign('user',$code);
    return view();
  }

  //积分消费
  public function jfspend($id)
  {
    $code = db('user')->find($id);
    if(request()->isAjax()){
      $data = input('post.');
      $user = new UserModel();
      $consume = $data['integral'];
      $num = $code['integral']-$data['integral'];
      if($num){
        $data['integral'] = $num;
      }else{
        $data['integral'] = '0';
      }
      $res = $user->allowField(true)->save($data,['id'=>input('id')]);
      if($res !== false){
        //写入日志
        addLog($uid = $user->id , $caozuo = '积分消费' , $balance = $consume);
        $this->success('消费成功');
      }else{
        $this->error('消费失败');
      }
      return;
    }
    if(!$code){
      $this->error("该用户不存在",'lst');
    }
    $this->assign('user',$code);
    return view();
  }

  //积分充值
  public function jfrecharge($id)
  {
    $code = db('user')->find($id);
    if(request()->isAjax()){
      $data = input('post.');
      $recharge = $data['integral'];
      $user = new UserModel();
      $res = $user->addRecharge($data,$id);
      if($res !== false){
        //写入日志
        addLog($uid = $user->id , $caozuo = '积分充值' , $balance = $recharge);
        $this->success('充值成功');
      }else{
        $this->error('消费失败');
      }
      return;
    }
    if(!$code){
      $this->error("该用户不存在",'lst');
    }
    $this->assign('user',$code);
    return view();
  }

  //日志
  public function log()
  {
    $log = new LogModel();
    $post = input('get');
    if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$post)) {
      $map['caozuo'] = input('get');
    } else {
      if (strlen($post) > 0 and $post) {
        $map['uid'] = input('get');
      }
    }
    $logs = empty($map) ? $log->order('id desc')->paginate(20) : $log->where($map)->order('id desc')->paginate($listRows = 20, $simple = false, $config = ['query'=>array('get'=>$post)]);
    $this->assign('logs',$logs);
    return view();
  }

}